﻿
//20220313
#include <iostream>
#include <opencv2\opencv.hpp>
#include <opencv2\imgproc\types_c.h>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
#include <tensorflow\c\c_api.h>
using namespace cv;
using namespace std;
string xmlPath = "C:\\Users\\11139\\Documents\\out\\install\\x64-Debug\\etc\\haarcascades\\haarcascade_frontalface_default.xml";
void detectAndDisplay(Mat image);
int main()
{
	printf("Hello from TensorFlow C library version %s\n", TF_Version());
	std::cout << "Hello World!\n";
	//读取视频或摄像头
	VideoCapture capture(0);

	while (true)
	{
		Mat frame;
		capture >> frame;
		//imshow("读取视频", frame);
        detectAndDisplay(frame);
		waitKey(10);	//延时30
	}
	return 0;

}

void detectAndDisplay(Mat image)
{
    CascadeClassifier ccf;      //创建脸部对象
    ccf.load(xmlPath);           //导入opencv自带检测的文件
    vector<Rect> faces;
    Mat gray;
    cvtColor(image, gray, CV_BGR2GRAY);
    equalizeHist(gray, gray);
    ccf.detectMultiScale(gray, faces, 1.1, 3, 0, Size(50, 50), Size(500, 500));
    for (vector<Rect>::const_iterator iter = faces.begin(); iter != faces.end(); iter++)
    {
        rectangle(image, *iter, Scalar(0, 0, 255), 2, 8); //画出脸部矩形
    }
    Mat image1;

    for (size_t i = 0; i < faces.size(); i++)
    {
        Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
        image1 = image(Rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height));
    }
    
    imshow("1", image);
    if (faces.size() != 0)
    {
    
        resize(image1,image1,Size(500,500));
        imshow("face", image1);
}
}


